﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBG_DBM.Support.DAL;
using GBG_DBM.Support.DAL.Entities;

namespace GBG_DBM.UI
{
    public partial class Freight_UI : Form
    {
        private Freight _Freight = new Freight();
        private DBAccessory _DBAccessory = new DBAccessory();
        public Freight_UI()
        {
            InitializeComponent();
        }
        public Freight_UI(Freight Freight) 
        {
            InitializeComponent();
            _Freight = Freight;
            DataBind();

        }
        private void DataBind()
        {
            if (_Freight.FreightType == null)
            {
                txtFreightType.Text = _Freight.FreightType;
                txtEstimateCost.Text = _Freight.Estimated.ToString();
                txtSupplier.Text = _Freight.Supplier;
                txtOrigin.Text = _Freight.Origin;
                txtRateType.Text = _Freight.RateType;
                txtPackagingWeight.Text = _Freight.PackagingWeight.ToString();
                txtProductWeight.Text = _Freight.ProductWeight.ToString();
                lblTotalWeight.Text = _Freight.Total.ToString();
                txtSupplierPrice.Text = _Freight.SupplierPrice.ToString();
                txtVariableFuelComponent.Text = _Freight.VariableFuel.ToString();
            }
            else 
            {
                txtFreightType.Text = _Freight.FreightType;
                txtFreightType.Enabled = false;
                txtEstimateCost.Text = _Freight.Estimated.ToString();
                txtSupplier.Text = _Freight.Supplier;
                txtOrigin.Text = _Freight.Origin;
                txtRateType.Text = _Freight.RateType;
                txtPackagingWeight.Text = _Freight.PackagingWeight.ToString();
                txtProductWeight.Text = _Freight.ProductWeight.ToString();
                lblTotalWeight.Text = _Freight.Total.ToString();
                txtSupplierPrice.Text = _Freight.SupplierPrice.ToString();
                txtVariableFuelComponent.Text = _Freight.VariableFuel.ToString();
            }

        }

        private void Cancel_Freight_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Add_Freight_UI_Click(object sender, EventArgs e)
        {
            if (_Freight.FreightType == null)
            {
                _Freight.FreightType = txtFreightType.Text;
                _Freight.RateType = txtRateType.Text;
                _Freight.Supplier = txtSupplier.Text;
                _Freight.PackagingWeight = Convert.ToSingle(txtPackagingWeight.Text);
                _Freight.ProductWeight = Convert.ToSingle(txtProductWeight.Text);
                _Freight.Origin = txtOrigin.Text;
                _Freight.SupplierPrice = Convert.ToInt32(txtSupplierPrice.Text);
                _Freight.VariableFuel = Convert.ToSingle(txtVariableFuelComponent.Text);
                _Freight.Estimated = _Freight.EstimatedCost();
                _Freight.Total = _Freight.ProductWeight + _Freight.PackagingWeight;
                _DBAccessory.addFreight(_Freight);
                this.Close();
            }
            else
            {
                _Freight.RateType = txtRateType.Text;
                _Freight.Supplier = txtSupplier.Text;
                _Freight.PackagingWeight = Convert.ToSingle(txtPackagingWeight.Text);
                _Freight.ProductWeight = Convert.ToSingle(txtProductWeight.Text);
                _Freight.Origin = txtOrigin.Text;
                _Freight.SupplierPrice = Convert.ToInt32(txtSupplierPrice.Text);
                _Freight.VariableFuel = Convert.ToSingle(txtVariableFuelComponent.Text);
                _Freight.Estimated = _Freight.EstimatedCost();
                _Freight.Total = _Freight.ProductWeight + _Freight.PackagingWeight;
                _DBAccessory.setFreight(_Freight);
                this.Close();
            }

        }
    }
}
